<template>
  <el-dialog
    title="积分历史记录"
    @close="onClose"
    v-model="visible"
    width="1000px"
    :close-on-click-modal="false"
    class="container"
  >
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="领取时间区间" prop="lingqushijian">
        <el-date-picker
          style="width: 240px"
          v-model="queryParams.lingqushijian"
          type="daterange"
          range-separator="~"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" size="default" :data="dataList">
      <el-table-column label="订单编号" min-width="150px" align="center" prop="orderId" />
      <el-table-column label="操作类型" align="center" prop="type">
        <template #default="{ row }">
          <div>
            {{ row?.type === 1 ? '积分获取' : '积分消耗' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="type">
        <template #default="{ row }">
          <div>
            {{ row?.status === 1 ? '订单完成' : '订单退款' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作时间"
        show-overflow-tooltip
        min-width="155px"
        align="center"
        prop="createTime"
      />
      <el-table-column label="相关商品" show-overflow-tooltip align="center" prop="comboName" />
      <el-table-column label="积分变动" align="center" prop="integral">
        <template #default="{ row }">
          {{
            row?.type === 1 && row?.integral
              ? '+' + row?.integral
              : row?.type === 2 && row?.integral
              ? '-' + row?.integral
              : row?.integral
          }}
        </template>
      </el-table-column>

      <el-table-column label="剩余积分" align="center" prop="surplusIntegral" />
    </el-table>
    <div style="margin-right: 10px">
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </el-dialog>
</template>

<script setup>
import { getPointRecordListApi } from '@/api/shopping'

const queryRef = ref(null)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  nickName: '',
  lingqushijian: ''
})
const visible = ref(false)
const total = ref(0)
const dataList = ref([])
const loading = ref(false)

// ======================= 搜索 ======================
function getList() {
  loading.value = false
  const params = { ...queryParams }
  params.startTime = params.lingqushijian?.length ? params.lingqushijian[0] + ' 00:00:00' : ''
  params.endTime = params.lingqushijian?.length ? params.lingqushijian[1] + ' 23:59:59' : ''
  delete params.lingqushijian
  getPointRecordListApi(params)
    .then((res) => {
      loading.value = false
      dataList.value = res?.rows || []
      total.value = res?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}
// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
}

function init(clientId, cityName) {
  visible.value = true
  queryParams.clientId = clientId
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
</style>
